//
//  FinishCheckedViewController.m
//  ICrEAM
//
//  Created by mac on 16/11/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "FinishCheckedViewController.h"
#import "FinishCheckSearchCell.h"
#import "FinishCheckDetailController.h"
#import "FinishListAction.h"
#import "FinishListRespose.h"

@interface FinishCheckedViewController ()<UITableViewDelegate>{
    NSString *_searchText;
    //当前页
    NSInteger page;
    NSInteger prepage;
}
//终检数据源
@property(nonatomic,strong)NSMutableArray *checkList;

@end
//12781001 是
//12781002 否
@implementation FinishCheckedViewController
-(NSMutableArray *)checkList{
    if (!_checkList) {
        _checkList=[NSMutableArray array];
    }
    return _checkList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSection];
    self.view.backgroundColor=[UIColor whiteColor];
    // Do any additional setup after loading the view.
}
-(void)setUpSection{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    view.backgroundColor=RGB(230, 236, 236);
    self.searchTextField.frame=CGRectMake(ScreenWidth-380, 4, 340, 36);
    self.searchTextField.placeholder=@"车牌号、送修人姓名、送修人手机、服务顾问";
    [view addSubview:self.searchTextField];
    self.switchArray=@[@"终检竣工"];
    self.mySwitch.frame=CGRectMake(0,0,300,44);
    [view addSubview: self.mySwitch];
    [ self.mySwitch selectIndex:0 animated:YES];
    [self.view addSubview:view];
    self.tableView.backgroundColor=[UIColor clearColor];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.frame=CGRectMake(0, CGRectGetMaxY(view.frame), ScreenWidth, ScreenHeight-110);
    [self.view addSubview:self.tableView];
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        prepage=page;
        page=1;
        [self rquestFinishCheckListIshead:YES];
    }];
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        prepage=page;
        [self rquestFinishCheckListIshead:NO];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark 获取终检列表数据
-(void)rquestFinishCheckListIshead:(BOOL)ishead{
    FinishListAction *action=[[FinishListAction alloc]init];
    action.current_page=[NSString stringWithFormat:@"%d",page];
    if (!IsStrEmpty(_searchText)) {
        action.serachText=_searchText;
    }
    [self requestDataWithAction:action haveHud:NO showError:NO  success:^(__kindof YTKBaseRequest *request) {
        [self.tableView.mj_header endRefreshing];
        id  keyValues = [request.responseString mj_JSONObject];
        if (![keyValues isKindOfClass:[NSDictionary class]]) {
            return;
        }
        if (ishead) {
            [self.checkList removeAllObjects];
        }
        page++;
        NSArray *list=request.responseJSONObject[@"TT_REPAIR_ORDER"];
        if (!IsArrEmpty(list)) {
            for (NSDictionary *dic in list) {
                FinishListRespose *repose=[FinishListRespose mj_objectWithKeyValues:dic];
                [self.checkList addObject:repose];
            }
            [self.tableView.mj_footer endRefreshing];
        }else{
            if (ishead) {
                [self showText:@"没有更多结果"];  
            }
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        [self.tableView reloadData];
        
    } failure:^(__kindof YTKBaseRequest *request) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        page=prepage;
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    _searchText=textField.text;
    [self.tableView.mj_header beginRefreshing];
    [textField resignFirstResponder];
    return YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.tableView.height/7.;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.checkList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FinishCheckSearchCell *cell=[FinishCheckSearchCell createFinishCheckSearchCell];
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0,self.tableView.height/7.-1, ScreenWidth, 1)];
    line.backgroundColor=RGB(238, 238, 238);
    [cell.contentView addSubview:line];
    FinishListRespose *repose=self.checkList[indexPath.row];
    cell.checkModel=repose;
    @weakify(self);
    cell.detailBlock=^(FinishListRespose *model){
#pragma mark 跳转详情页
        @strongify(self);
        FinishCheckDetailController *detail=[[FinishCheckDetailController alloc]init];
        detail.model=model;
        [self.navigationController pushViewController:detail animated:YES];
    };
    return cell;
}
#pragma mark 跳转编辑页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FinishListRespose *repose=self.checkList[indexPath.row];
    if (repose.isEdit) {
        FinishCheckDetailController *detail=[[FinishCheckDetailController alloc]init];
        detail.delegate=self;
        detail.model=repose;
        detail.isEdit=YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
    
}
-(void)finishCheckSucess{
    [self.tableView.mj_header beginRefreshing];
}
@end
